<?php
require_once 'Zend/Db/Table/Abstract.php';
require_once APPLICATION_PATH . '/models/ContentNode.php';

class Application_Model_Page extends Zend_Db_Table_Abstract
{
	/**
	 * The default table name
	 */
	protected $_name = 'pages';
	protected $_dependentTables = array('Model_ContentNode');
	protected $_referenceMap = array(
		'Page' => array(
			'columns'			=> array('page_id'),
			'refTableClass'	=> 'Model_Page',
			'refColumns'   	=> array('id'),
			'onDelete'			=> self::CASCADE,
			'onUpdate'        => self::RESTRICT	
		)
	);
	
	public function createPage($name, $namespace, $parentid = 0)
	{	
		$row = $this->createRow();
		$row->name = $name;
		$row->namespace = $namespace;
		$row->parent_id = $parentid;
		$row->date_created = time();
		
		$row->save();
		$id = $this->_db->lastInsertId();
		return $id;	
	}
	
	public function deletePage($id)
	{
		$row = $this->find($id)->current();
		if($row) {
			$row->delete();
			return true;
		} else {
			throw new Exception("Delete function failed; could not found page!");
		}
	}

}

